package com.basic.modules.ykf.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.validator.ValidatorUtils;

import com.basic.modules.ykf.entity.RuleItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.basic.modules.ykf.entity.YkfShopDistributeRuleEntity;
import com.basic.modules.ykf.service.YkfShopDistributeRuleService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;



/**
 * 店铺分配规则
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-05-19 21:27:24
 */
@RestController
@RequestMapping("ykf/shopDistributeRule")
public class YkfShopDistributeRuleController {

    @Autowired
    private YkfShopDistributeRuleService ykfShopDistributeRuleService;

    /**
     * 列表-分页
     */
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfShopDistributeRuleEntity> params){
        PageUtils page = ykfShopDistributeRuleService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfShopDistributeRuleEntity params){
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getRuleId())) {
            throw new RRException("参数缺失");
        }
        YkfShopDistributeRuleEntity byId = ykfShopDistributeRuleService.getById(params.getRuleId());
        if (ObjectUtil.isNotEmpty(byId) && StrUtil.isNotBlank(byId.getRuleJson())) {
            List<RuleItem> list = JSONUtil.toList(JSONUtil.parseArray(byId.getRuleJson()), RuleItem.class);
            byId.setRuleList(list);
        }
        return R.ok(byId);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfShopDistributeRuleEntity ykfShopDistributeRule){
        ValidatorUtils.validateEntity(ykfShopDistributeRule);
        ykfShopDistributeRule.setRuleJson(JSONUtil.toJsonStr(ykfShopDistributeRule.getRuleList()));
		ykfShopDistributeRuleService.save(ykfShopDistributeRule);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfShopDistributeRuleEntity ykfShopDistributeRule){
        ValidatorUtils.validateEntity(ykfShopDistributeRule);
        ykfShopDistributeRule.setRuleJson(JSONUtil.toJsonStr(ykfShopDistributeRule.getRuleList()));
		ykfShopDistributeRuleService.updateById(ykfShopDistributeRule);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] distributeRuleIds){
		ykfShopDistributeRuleService.removeByIds(Arrays.asList(distributeRuleIds));
        return R.ok();
    }


    /**
     * 设置默认
     */
    @PostMapping("/setDefault")
    public R setDefault(@RequestBody YkfShopDistributeRuleEntity ykfShopDistributeRule){
        if (ObjectUtil.isNotEmpty(ykfShopDistributeRule.getRuleId())) {
            UpdateWrapper<YkfShopDistributeRuleEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().set(YkfShopDistributeRuleEntity::getDefaultFlag, 0);
            ykfShopDistributeRuleService.update(updateWrapper);
            UpdateWrapper<YkfShopDistributeRuleEntity> updateWrapper1 = new UpdateWrapper<>();
            updateWrapper1.lambda().eq(YkfShopDistributeRuleEntity::getRuleId, ykfShopDistributeRule.getRuleId())
                    .set(YkfShopDistributeRuleEntity::getDefaultFlag, 1);
            ykfShopDistributeRuleService.update(updateWrapper1);
        } else {
            throw new RRException("参数缺失");
        }
        return R.ok();
    }


    /**
     * 客服检测
     */
    @PostMapping("/customerCheck")
    public R customerCheck(@RequestBody YkfShopDistributeRuleEntity ykfShopDistributeRule){
        if (ObjectUtil.isNotEmpty(ykfShopDistributeRule.getRuleId())) {

        } else {
            throw new RRException("参数缺失");
        }
        return R.ok();
    }

}
